<?php


namespace app\portal\model;

use think\db\Query;
use think\Model;

class WorksModel extends Model
{
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'works';

    public function comm() {
        return $this->hasMany('ScoreModel','work_id','id');
    }

    /**
     * 获取全部作品列表
     * @param $filter
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function adminWorkList($filter) {
        $user_id = cmf_get_current_admin_id();
        $userModel = new UserModel();
        $userinfo = $userModel->find($user_id);
        $field = 'w.*,c.title as contest_title,c.user_id,c.apple_start,c.apple_end,c.review_start,c.review_end,u.user_nickname,u.user_login,u.mobile';
        $workQuery = $this->alias('w');
        $workQuery->join('contest c', 'c.id = w.contest_id');
        $workQuery->join('user u', 'w.user_id = u.id');
        if($userinfo['user_type'] == 2) {
            $workQuery->where('w.user_id',$user_id);
        }

        $author = empty($filter['author']) ? "" : $filter['author'];
        $source   = empty($filter['source']) ? "" : $filter['source'];
        if (!empty($author)) {
            $usersId = $userModel->where('user_nickname','like','%'.$author.'%')->field('id')->select()->toArray();
            $s = array_column($usersId,'id');
            $workQuery->whereIn('w.user_id',$s);
        }
        if (!empty($source)) {
            $usersId = $userModel->where('user_url','like','%'.$source.'%')->field('id')->select()->toArray();
            $s = array_column($usersId,'id');
            $workQuery->whereIn('w.user_id',$s);
        }

//        return $filter;
        $works = $workQuery->field($field)
            ->where('w.status',1)
            ->where('w.delete_time', 0)
            ->where(function (Query $query) use ($filter) {
                $category   = empty($filter['category']) ? 0 : $filter['category'];
                if(!empty($category)) {
                    $query->where('w.contest_id',  $filter['category']);
                }
            })
//            ->order('w.score','ASC')
            ->order('w.update_time', 'DESC')
            ->paginate(10);
        return $works;
    }

    /**
     * 获取全部作品导出数据
     * @param $filter
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function adminWorkListExport($filter) {
        $user_id = cmf_get_current_admin_id();
        $userModel = new UserModel();
        $userinfo = $userModel->find($user_id);
        $field = 'w.*,c.title as contest_title,c.id as contest_id,c.user_id,c.apple_start,c.apple_end,c.review_start,c.review_end,u.user_nickname,u.user_login,u.mobile,u.user_url';
        $workQuery = $this->alias('w');
        $workQuery->join('contest c', 'c.id = w.contest_id');
        $workQuery->join('user u', 'w.user_id = u.id');
        if($userinfo['user_type'] == 2) {
            $workQuery->where('w.user_id',$user_id);
        }

        $author = empty($filter['author']) ? "" : $filter['author'];
        $source   = empty($filter['source']) ? "" : $filter['source'];
        if (!empty($author)) {
            $usersId = $userModel->where('user_nickname','like','%'.$author.'%')->field('id')->select()->toArray();
            $s = array_column($usersId,'id');
            $workQuery->whereIn('w.user_id',$s);
        }
        if (!empty($source)) {
            $usersId = $userModel->where('user_url','like','%'.$source.'%')->field('id')->select()->toArray();
            $s = array_column($usersId,'id');
            $workQuery->whereIn('w.user_id',$s);
        }

//        return $filter;
        $works = $workQuery->field($field)
            ->where('w.status',1)
            ->where('w.delete_time', 0)
            ->where(function (Query $query) use ($filter) {
                $category   = empty($filter['category']) ? 0 : $filter['category'];
                if(!empty($category)) {
                    $query->where('w.contest_id',  $filter['category']);
                }
            })
//            ->order('w.score','ASC')
            ->order('w.update_time', 'DESC')->select()->toArray();
        return $works;
    }

    /**
     * 获取赛事的全部作品
     * @param $filter
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getWorkList($filter) {
        $field = 'w.*,c.title as contest_title,c.user_id,c.apple_start,c.apple_end,c.review_start,c.review_end,cc.name as c_name,u.user_nickname,u.user_login,u.mobile,s.score as s_score';
        $reviewId = $filter['review_id'];
        $workQuery = $this->alias('w');
        $workQuery->join('contest c', 'c.id = w.contest_id');
        $workQuery->join('user u', 'w.user_id = u.id');
        $workQuery->join('contest_category cc','w.contest_category = cc.id');
        $workQuery->join('score s',"w.id = s.work_id and s.review_id = $reviewId",'left');

        if($filter['contest_category'] != 0) {
            $workQuery->where('w.contest_category',$filter['contest_category']);
        }

        $works = $workQuery->field($field)
            ->where('w.status',1)
            ->where('w.delete_time', 0)
            ->where('w.contest_id',$filter['contest_id'])
//            ->order('w.score','ASC')
            ->order('w.create_time', 'DESC')
            ->paginate(10);
        return $works;
    }

    /**
     * 获取评委评分的作品
     * @param $filter
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getReviewList($uid) {
        $field = 'w.*,u.user_login,u.mobile,c.title as c_title,c.review_start,c.review_end,cc.name as cate_name';
        $workQuery = $this->alias('w');
        $workQuery->join('contest c', 'c.id = w.contest_id');
        $workQuery->join('user u', 'w.user_id = u.id');
        $workQuery->join('contest_category cc','w.contest_category = cc.id');

        $works = $workQuery->field($field)
            ->where('w.review_id',$uid)
//            ->order("w.score = 0 ASC")
            ->order('w.update_time', 'DESC')
            ->paginate(10);
        return $works;
    }

    /**
     * 上传作品
     * @param $data
     * @return $this
     */
    public function adminAddWork($data) {
        $data['user_id'] = isset($data['user_id'])?$data['user_id']:cmf_get_current_admin_id();

        if (!empty($data['more']['thumbnail'])) {
            $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
            $data['thumbnail']         = $data['more']['thumbnail'];
        }

        if (!empty($data['more']['audio'])) {
            $data['more']['audio'] = cmf_asset_relative_url($data['more']['audio']);
        }

        if (!empty($data['more']['video'])) {
            $data['more']['video'] = cmf_asset_relative_url($data['more']['video']);
        }
        $data['more'] = json_encode($data['more']);
        $this->save($data);

        return $this;
    }

    public function adminEditWork($data) {
        if (!empty($data['more']['thumbnail'])) {
            $data['more']['thumbnail'] = cmf_asset_relative_url($data['more']['thumbnail']);
            $data['thumbnail']         = $data['more']['thumbnail'];
        }

        if (!empty($data['more']['audio'])) {
            $data['more']['audio'] = cmf_asset_relative_url($data['more']['audio']);
        }

        if (!empty($data['more']['video'])) {
            $data['more']['video'] = cmf_asset_relative_url($data['more']['video']);
        }
        $data['more'] = json_encode($data['more']);
        $work = self::find($data['id']);

        $work->save($data);

        return $this;
    }
}